package com.monitor.ui;

import com.monitor.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

/**
 * 系统托盘图标管理类
 * 负责创建和管理系统托盘图标及其菜单
 */
public class TrayIconManager {
    private MainFrame mainFrame;
    private TrayIcon trayIcon;
    private SystemTray systemTray;
    private PopupMenu popupMenu;
    
    /**
     * 构造函数
     * @param mainFrame 主窗口引用
     */
    public TrayIconManager(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
        
        // 检查系统是否支持系统托盘
        if (!SystemTray.isSupported()) {
            System.out.println("系统不支持托盘功能");
            return;
        }
        
        systemTray = SystemTray.getSystemTray();
        createTrayIcon();
    }
    
    /**
     * 创建系统托盘图标和菜单
     */
    private void createTrayIcon() {
        // 创建弹出菜单
        popupMenu = new PopupMenu();
        
        // 添加菜单项
        MenuItem showItem = new MenuItem("显示");
        MenuItem protectionItem = new MenuItem("防护中心");
        MenuItem controlItem = new MenuItem("控制中心");
        MenuItem resourceItem = new MenuItem("资源中心");
        MenuItem maintenanceItem = new MenuItem("维护中心");
        MenuItem exitItem = new MenuItem("退出");
        
        // 添加事件监听器
        showItem.addActionListener(e -> {
            showMainFrame();
        });
        
        protectionItem.addActionListener(e -> {
            showMainFrame();
            mainFrame.showPanel("防护中心");
        });
        
        controlItem.addActionListener(e -> {
            showMainFrame();
            mainFrame.showPanel("控制中心");
        });
        
        resourceItem.addActionListener(e -> {
            showMainFrame();
            mainFrame.showPanel("资源中心");
        });
        
        maintenanceItem.addActionListener(e -> {
            showMainFrame();
            mainFrame.showPanel("维护中心");
        });
        
        exitItem.addActionListener(e -> {
            System.exit(0);
        });
        
        // 将菜单项添加到弹出菜单
        popupMenu.add(showItem);
        popupMenu.addSeparator();
        popupMenu.add(protectionItem);
        popupMenu.add(controlItem);
        popupMenu.add(resourceItem);
        popupMenu.add(maintenanceItem);
        popupMenu.addSeparator();
        popupMenu.add(exitItem);
        
        // 获取图标
        Image iconImage = null;
        try {
            // 尝试加载图标资源
            iconImage = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/protection.png"));
        } catch (Exception e) {
            // 如果加载失败，创建一个默认图标
            iconImage = createDefaultIconImage();
        }
        
        // 创建系统托盘图标
        trayIcon = new TrayIcon(iconImage, "金惠反黄专家", popupMenu);
        trayIcon.setImageAutoSize(true);
        
        // 添加鼠标事件监听器，双击显示主窗口
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    showMainFrame();
                }
            }
        });
        
        // 将图标添加到系统托盘
        try {
            systemTray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println("无法将图标添加到系统托盘: " + e.getMessage());
        }
    }
    
    /**
     * 显示主窗口
     */
    private void showMainFrame() {
        if (mainFrame != null) {
            mainFrame.setVisible(true);
            mainFrame.setState(Frame.NORMAL);
            mainFrame.toFront();
        }
    }
    
    /**
     * 创建默认图标图像
     * @return 默认图标图像
     */
    private Image createDefaultIconImage() {
        // 创建一个16x16的默认图标
        BufferedImage image = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setColor(new Color(0, 120, 215));
        g2d.fillRect(0, 0, 16, 16);
        g2d.setColor(Color.WHITE);
        g2d.drawString("JH", 2, 12);
        g2d.dispose();
        return image;
    }
    
    /**
     * 显示托盘通知消息
     * @param title 通知标题
     * @param message 通知内容
     * @param type 通知类型
     */
    public void displayMessage(String title, String message, TrayIcon.MessageType type) {
        if (trayIcon != null) {
            trayIcon.displayMessage(title, message, type);
        }
    }
    
    /**
     * 最小化到托盘
     */
    public void minimizeToTray() {
        if (mainFrame != null) {
            mainFrame.setVisible(false);
        }
    }
}